<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="uid">
        <el-input v-model="queryParams.uid" placeholder="请输入用户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="应用名称" prop="appName">
        <el-input v-model="queryParams.appName" placeholder="请输入应用名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="渠道名称" prop="channelName">
        <el-input v-model="queryParams.channelName" placeholder="请输入渠道名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="省市区" prop="provincial">
        <el-input v-model="queryParams.provincial" placeholder="请输入省市区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item v-for="item in dynamicParamList" :key="item.filedName" :label="item.name" :prop="item.filedName">
        <el-select style="width: 100%" v-model="form[item.filedName]" :placeholder="`请选择${item.name}`"
          @change="handleQuery" :allow-clear="true" clearable>
          <el-option v-for="itemOp in item.children" :key="itemOp.value" :label="itemOp.name" :value="itemOp.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="sourceInfoList">
      <el-table-column label="用户ID" align="center" prop="uid" width="170" />
      <el-table-column label="个人信息" align="center" width="150">
        <template slot-scope="scope">
          {{ scope.row.trealname }}--{{ scope.row.sex === 1 ? '男' : '女' }}--{{ scope.row.age }}岁
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="tmobile" width="120"/>
      <el-table-column label="应用名称" align="center" prop="appName" />
      <el-table-column label="渠道名称" align="center" prop="channelName" />
      <el-table-column label="所属地" align="center" width="120">
        <template slot-scope="scope">
          {{ scope.row.province }}--{{ scope.row.city }}
        </template>
      </el-table-column>
      <el-table-column label="贷款金额" align="center" prop="loanAmount" />
      <el-table-column label="贷款时间" align="center" prop="loanTime" />
      <el-table-column label="逾期" align="center" prop="yuqi" />
      <el-table-column label="收入" align="center" prop="shouru" width="120"/>
      <el-table-column label="学历" align="center" prop="xueli" />
      <el-table-column label="贷款用途" align="center" prop="yongtu" />
      <el-table-column label="工作" align="center" prop="gongzuo" />
      <el-table-column label="房产" align="center" prop="house" />
      <el-table-column label="车产" align="center" prop="car" width="120"/>
      <el-table-column label="公积金" align="center" prop="gjj" />
      <el-table-column label="社保" align="center" prop="shebao" />
      <el-table-column label="商业保险" align="center" prop="baoxian" />
      <el-table-column label="芝麻分数" align="center" prop="zhima" width="90"/>
      <el-table-column label="花呗" align="center" prop="huabei" />
      <el-table-column label="白条" align="center" prop="baitiao" />
      <el-table-column label="微粒花" align="center" prop="weili" />
      <el-table-column label="信用卡" align="center" prop="xinyongka" />
      <el-table-column label="创建时间" align="center" width="135">
        <template slot-scope="scope">
          {{ formatTimestamp(scope.row.createTime) }}
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog title="输入用户ID" :visible="showDialog" @close="showDialog = false" width="30%">
      <el-input v-model="inputUid" placeholder="请输入用户ID"></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="handleDeleteAndUpdate">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listSourceInfo, addSourceInfo, updateSourceInfo, delAndupSourceInfo } from "@/api/member/sourceInfo";
import { listDictInfo } from "@/api/dict/dictInfo";
export default {
  name: "SourceInfo",
  data() {
    return {
      showDialog: false,
      inputUid: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户资质信息表格数据
      sourceInfoList: [],
      dynamicParamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateInfo: null,
        appCode: null,
        appName: null,
        channelCode: null,
        channelName: null,
        realname: null,
        mobile: null,
        mobileMd5: null,
        idcard: null,
        sex: null,
        age: null,
        reqIp: null,
        city: null,
        cityCode: null,
        province: null,
        provinceCode: null,
        loanAmount: null,
        loanTime: null,
        house: null,
        car: null,
        gjj: null,
        shebao: null,
        baoxian: null,
        zhima: null,
        huabei: null,
        baitiao: null,
        weili: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /**更改创建时间格式 */
    formatTimestamp(timestamp) {
      if (!timestamp) {
        return ''; // Return empty string if timestamp is falsy
      }
      const date = new Date(timestamp);
      // Customize the date format according to your preference
      const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
      return formattedDate;
    },
    /** 查询用户资质信息列表 */
    getList() {
      this.loading = true;
      listSourceInfo(this.queryParams).then(response => {
        this.sourceInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uid: null,
        dateInfo: null,
        appCode: null,
        appName: null,
        channelCode: null,
        channelName: null,
        realname: null,
        mobile: null,
        mobileMd5: null,
        idcard: null,
        sex: null,
        age: null,
        reqIp: null,
        city: null,
        cityCode: null,
        province: null,
        provinceCode: null,
        loanAmount: null,
        loanTime: null,
        house: null,
        car: null,
        gjj: null,
        shebao: null,
        baoxian: null,
        zhima: null,
        huabei: null,
        baitiao: null,
        weili: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;

      for (const item of this.dynamicParamList) {
        const camelCaseFieldName = item.filedName.replace(/_(\w)/g, (_, letter) => letter.toUpperCase());
        this.queryParams[camelCaseFieldName] = this.form[item.filedName];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      for (const item of this.dynamicParamList) {
        this.form[item.filedName] = null; // Clear the selected option
      }
      this.handleQuery();
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listDictInfo().then((response) => {
        this.dynamicParamList = this.handleTree(response.rows, "id");
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uid != null) {
            updateSourceInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSourceInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
